package com.mdd.admin.service.jobs.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.service.jobs.IJobsService;
import com.mdd.admin.validate.common.PageParam;
import com.mdd.admin.validate.jobs.JobsParam;
import com.mdd.admin.vo.jobs.JobsDetailVo;
import com.mdd.admin.vo.jobs.JobsListVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.jobs.Jobs;
import com.mdd.common.mapper.jobs.JobsMapper;
import com.mdd.common.utils.TimeUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 岗位实现类
 */
@Service
public class JobsServiceImpl implements IJobsService {
        
    @Resource
    JobsMapper jobsMapper;

    /**
     * 岗位列表
     *
     * @param pageParam 分页参数
     * @param params 搜索参数
     * @return PageResult<JobsListVo>
     */
    @Override
    public PageResult<JobsListVo> list(PageParam pageParam, Map<String, String> params) {
        Integer page  = pageParam.getPageNo();
        Integer limit = pageParam.getPageSize();

        QueryWrapper<Jobs> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc(Arrays.asList("sort", "id"));

        jobsMapper.setSearch(queryWrapper, params, new String[]{
            "like:name:str",
            "=:code:str",
            "=:sort:long",
            "=:status:int",
            "=:remark:str",
        });

        IPage<Jobs> iPage = jobsMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<JobsListVo> list = new LinkedList<>();
        for(Jobs item : iPage.getRecords()) {
            JobsListVo vo = new JobsListVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtil.timestampToDate(item.getCreateTime()));
            vo.setUpdateTime(TimeUtil.timestampToDate(item.getUpdateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 岗位详情
     *
     * @param id 主键参数
     * @return Jobs
     */
    @Override
    public JobsDetailVo detail(Integer id) {
        Jobs model = jobsMapper.selectOne(
                new QueryWrapper<Jobs>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        JobsDetailVo vo = new JobsDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 岗位新增
     *
     * @param jobsParam 参数
     */
    @Override
    public void add(JobsParam jobsParam) {
        Jobs model = new Jobs();
        model.setName(jobsParam.getName());
        model.setCode(jobsParam.getCode());
        model.setSort(jobsParam.getSort());
        model.setStatus(jobsParam.getStatus());
        model.setRemark(jobsParam.getRemark());
        jobsMapper.insert(model);
    }

    /**
     * 岗位编辑
     *
     * @param jobsParam 参数
     */
    @Override
    public void edit(JobsParam jobsParam) {
        Jobs model = jobsMapper.selectOne(
                new QueryWrapper<Jobs>()
                    .eq("id",  jobsParam.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(jobsParam.getId());
        model.setName(jobsParam.getName());
        model.setCode(jobsParam.getCode());
        model.setSort(jobsParam.getSort());
        model.setStatus(jobsParam.getStatus());
        model.setRemark(jobsParam.getRemark());
        jobsMapper.updateById(model);
    }

    /**
     * 岗位删除
     *
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        Jobs model = jobsMapper.selectOne(
                new QueryWrapper<Jobs>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        jobsMapper.delete(new QueryWrapper<Jobs>().eq("id", id));
    }

}
